package com.szalay.desktopsearch.ui;

import com.szalay.desktopsearch.search.Search;
import com.szalay.desktopsearch.search.SearchListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import org.apache.solr.client.solrj.response.QueryResponse;

/**
 * The scroller component to scroll through the search results.
 * @author szalay
 */
public class Scroller extends JPanel implements SearchListener {
    private static final long serialVersionUID = -4657331570647318601L;

    /** Page size. */
    public static final int PAGE_SIZE = 5;
    
    /** Max pages. */
    private static final int MAX_PAGES = 10;
    
    //Current page
    private volatile int currentPage = 0;
    
    //Current num pages
    private volatile int currentNumberOfPages = MAX_PAGES;
    
    //The term
    private volatile String term;
    
    //Search service
    private final Search search;
    
    /** Create a new scroller component. */
    public Scroller(final Search search) {
        super(false);
        this.search = search;
        setBorder(BorderFactory.createLoweredBevelBorder());
    }
    
    @Override
    public void searchStarting(String term) {
        removeAll();
        this.term = term;
    }

    @Override
    public synchronized void searchFinished(final QueryResponse response) {
        removeAll();
        
        //Create the scroller components
        final long numResults = response.getResults().getNumFound();
        if (numResults < PAGE_SIZE) {
            return;
        }
        
        final long numPages = numResults / PAGE_SIZE;
        for (int i = 0; i < numPages; i++) {
            if (i == currentNumberOfPages) {
                break;
            }
            final JButton button = new JButton(Integer.valueOf(i + 1).toString());
            if (currentPage == i) {
                button.setEnabled(false);
            }
            else {
                button.setEnabled(true);
            }
            
            final int pageForThisButton = i;
            button.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseReleased(MouseEvent e) {
                    currentPage = pageForThisButton;
                    final SwingWorker worker = new SwingWorker() {
                        @Override
                        protected Object doInBackground() throws Exception {
                            return search.search(term, pageForThisButton * PAGE_SIZE, currentNumberOfPages);
                        }
                    };
                    worker.run();
                }
            });
            
            add(button);
        }
        
        //Display a show more button if there are still more pages
        if (numPages > (PAGE_SIZE * currentNumberOfPages)) {
            final JButton showMoreButton = new JButton("...");
            showMoreButton.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseReleased(MouseEvent e) {
                    currentNumberOfPages += 10;
                    final SwingWorker worker = new SwingWorker() {
                        @Override
                        protected Object doInBackground() throws Exception {
                            return search.search(term, currentPage * PAGE_SIZE, currentNumberOfPages);
                        }
                    };
                    worker.run();
                }
            });
            add(showMoreButton);
        }
    }
}
